<?php
$publish_btn = '<span><i class="icon-rss"></i> '.BUtils::T('publish_btn').'</span>';
$publish_title = BUtils::T('special_publish_eg');

$edit_btn = '<i class="icon-edit icon-only bigger-150"></i>';
$edit_title = BUtils::T('special_edit_eg');
$delete_btn = '<i class="icon-remove-sign icon-only bigger-150"></i>';
$delete_title = BUtils::T('special_delete_eg');
$new_offer_btn = '<i class="icon-plus-sign icon-only bigger-150"></i>';
$new_offer_title = BUtils::T('special_add_eg');
//share to facebook: only for published offer
$fb_share_btn = '<i class="icon-facebook-sign icon-only bigger-150"></i>';
$fb_share_title = BUtils::T('special_fb_share_eg');
//pop up a report for this special.
$view_report_btn = '<i class="icon-bar-chart icon-only bigger-150"></i>';
$view_report_title = BUtils::T('special_report_eg');
$view_report_title_disabled = BUtils::T('special_report_not_ready_eg');


$reach_lbl = '<span class="" rel="tipsy" title="'.BUtils::T('special_report_reach_eg').'">' . BUtils::T('special_report_reach_lbl') . '</span>';
$clicks_lbl = '<span class="" rel="tipsy" title="'.BUtils::T('special_report_clicks_eg').'">' . BUtils::T('special_report_clicks_lbl') . '</span>';
$notifications_lbl = '<span class="" rel="tipsy" title="'.BUtils::T('special_report_notifications_eg').'">' . BUtils::T('special_report_notifications_lbl') . '</span>';
$redeem_lbl = '<span class="" rel="tipsy" title="'.BUtils::T('special_report_redeems_eg').'">' . BUtils::T('special_report_redeems_lbl') . '</span>';

$expired_lbl = BUtils::T('expired_lbl');
$unpublished_lbl = BUtils::T('unpublished_lbl');
$special_active_lbl = BUtils::T('special_active_lbl');
$special_pending_lbl = BUtils::T('special_pending_lbl');
$special_completed_lbl = BUtils::T('special_completed_lbl');

$predefined_days = BUtils::WeekDayKeys(7, true);

echo '<table class="table table-striped table-hover">';
foreach ($model as $k => $r) {
	//set offer picture
	$url_thumb_default = Yii::app()->baseUrl . '/img/default_special_thumb.png';
	$url_picture_default = Yii::app()->baseUrl . '/img/default_special_picture.png';
	$url_thumb = empty($r['url_picture']) ? $r['url_offer_img'] : $r['url_picture'];
	if (!empty($url_thumb)) {
		$url_thumb = BUtils::GetOfferThumbUrl($url_thumb);
		$url_picture = BUtils::GetOfferPictureUrl(str_replace('thumb_', '', $url_thumb));
	} else {
		$url_thumb = $url_thumb_default;
		$url_picture = $url_picture_default;
	}
	
	//offer valid time
	$range_time = BUtils::GetHourRangeFormat($r['start_hour'], $r['end_hour'], $r['show_hours']);
	//offer valid date
	$range_date = BUtils::GetDateRangeString($r['uts_start_date'], $r['uts_end_date']);
	//offer valid date
	$range_week = BUtils::GetWeekdays($r['week_days'], $predefined_days);
	
	//set price, discount or highlight
	$highlight = $del_price = '';
	if (!empty($r['price'])) {
		$highlight = Yii::app()->locale->numberFormatter->formatCurrency($r['price'], 'EUR');
		if (!empty($r['value'])) {
			$value = Yii::app()->locale->numberFormatter->formatCurrency($r['value'], 'EUR');
			$del_price = '<small><s class="gray">'.$value.'</s></small>';
		}
	} elseif (!empty($r['discount'])) {
		$highlight = MerchantObject::formatDiscount($r['discount']);
	}
	
	//offer content
	$content = $body = strip_tags(trim($r['content']));
	if (strlen($content) > 108) {
		$match = null;
		if (preg_match('/^.{1,108}\b/s', $body, $match)) {
			$content = $match[0] . ' ...';
		}
	}
	
	//set expired or upcoming offer style
	$now = time();
	$status_label = '<span class="label label-info arrowed-in-right"><i class="icon-play"></i> ' . $special_active_lbl . '</span>';
	if ($r['status'] == 0) {
		if ($now < $r['uts_end_date']) $status_label = '<span class="label label-important arrowed-in-right"><i class="icon-warning-sign"></i> ' . $unpublished_lbl . '</span>';
		else $status_label = '<span class="label label-important arrowed-in-right"><i class="icon-warning-sign"></i> ' . $expired_lbl . '</span>';
	} elseif ($now < $r['uts_start_date']) {
		$status_label = '<span class="label label-yellow arrowed-in-right"><i class="icon-time"></i> ' . $special_pending_lbl . '</span>';
	} elseif ($now > $r['uts_end_date']) {
		$status_label = '<span class="label label-light arrowed-in-right"><i class="icon-ok"></i> ' . $special_completed_lbl . '</span>';
	}
	
	if ($now < $r['uts_start_date']) {
		$ex_days = 0;//pending status
	} elseif ($now > $r['uts_end_date']) {
		$ex_days = round(($r['uts_end_date'] - $r['uts_start_date']) / 3600 / 24);
	} else {
		$ex_days = round((time() - $r['uts_start_date']) / 3600 / 24);
	}
	
	if ($ex_days === 0) {
		//pending status
		$reach = $clicks = $notifications = $spread = $redeems = '-';
	} else {
		$clicks = round($r['clicks']);
		$notifications = round($r['notifications']);
		$spread = round($r['spread']);
		$redeems = round($r['redeems']);
		$reach = round($r['reach']);
		if ($reach < 1) {
			$reach = $clicks = $notifications = $spread = $redeems = '-';
		}
	}
	
	$fb_like_button = '';
	if ($now < $r['uts_end_date'] and $r['status'] == 1) {
		$fb_like_button = '<a data-url="http://yamii.fi/special/'.$r['id'].'" class="btn btn-mini fb-like-callback" rel="tipsy" title="'.$fb_share_title.'">' . $fb_share_btn . '</a>';
	}
?>
<tr class="hover-action">
	<td class="center" style="width:70px;">
		<a href="<?php echo Yii::app()->createUrl('/offer/preview', array('id'=>$r['id'])) ?>" class="popup-btn">
			<img class="img-polaroid" style="width:64px;" src="<?php echo $url_thumb; ?>" alt="" onError="this.onerror=null;this.src='<?php echo $url_thumb_default; ?>';" />
		</a>
	</td>
	<td>
		<h4 class="orange"><?php echo $highlight; ?> <?php echo $del_price; ?></h4>
		<a href="<?php echo Yii::app()->createUrl('/offer/preview', array('id'=>$r['id'])) ?>" class="popup-btn"><?php echo $content; ?></a>
	</td>
	<td style="width:180px;">
		<?php echo $status_label; ?>
		<div class="date"><?php echo $range_date; ?></div>
		<div class="time"><?php echo $range_week; ?></div>
		<div class="time"><?php echo $range_time; ?></div>
	</td>
	<td style="width:130px;font-size:11px;line-height:13px;text-align:center;">
<?php if ($reach == '-') { ?>
		<div class="center" style="color:#cde;">
			<i class="icon-gears icon-4x"></i>
		</div>
		<div class="hr hr4"></div>
		<span style="color:#888;"><?php echo $view_report_title_disabled; ?></span>
<?php } else { ?>
		<div class="row-fluid">
			<div class="span6 pointer"><?php echo $reach; ?><br /><?php echo $reach_lbl; ?></div>
			<div class="span6 pointer"><?php echo $clicks; ?><br /><?php echo $clicks_lbl; ?></div>
		</div>
		<div class="hr hr4"></div>
		<div class="row-fluid">
			<div class="span6 pointer"><?php echo $notifications; ?><br /><?php echo $notifications_lbl; ?></div>
			<div class="span6 pointer"><?php echo $redeems; ?><br /><?php echo $redeem_lbl; ?></div>
		</div>
<?php } ?>
	</td>
	<td style="text-align:right;width:150px;">
<?php
if (empty($r['status'])) {
	echo '<div class="hover-show">';
	if ($reach == '-') echo CHtml::link($view_report_btn, 'javascript:;', array('class'=>'btn btn-mini disabled', 'rel'=>'tipsy', 'title'=>$view_report_title_disabled));
	else echo CHtml::link($view_report_btn, Yii::app()->createUrl('/offer/reportDaily', array('id'=>$r['id'])), array('class'=>'btn btn-mini popup-btn', 'rel'=>'tipsy', 'title'=>$view_report_title));
	echo CHtml::link($edit_btn, Yii::app()->createUrl('/offer/form', array('id'=>$r['id'])), array('class'=>'btn btn-mini unpub', 'rel'=>'tipsy', 'title'=>$edit_title));
	echo CHtml::link($delete_btn, Yii::app()->createUrl('/offer/delete', array('id'=>$r['id'])), array('class'=>'btn btn-mini delete-btn', 'rel'=>'tipsy', 'title'=>$delete_title));
	echo '</div><div class="space-6"></div>';
	echo CHtml::link($publish_btn, Yii::app()->createUrl('/offer/publish', array('id'=>$r['id'])), array('class'=>'btn btn-primary publish-btn unpub', 'rel'=>'tipsy', 'title'=>$publish_title));
} elseif ($r['status'] == 1) {
	echo '<div class="hover-show">';
	echo $fb_like_button;
	if ($reach == '-') echo CHtml::link($view_report_btn, 'javascript:;', array('class'=>'btn btn-mini disabled', 'rel'=>'tipsy', 'title'=>$view_report_title_disabled));
	else echo CHtml::link($view_report_btn, Yii::app()->createUrl('/offer/reportDaily', array('id'=>$r['id'])), array('class'=>'btn btn-mini popup-btn', 'rel'=>'tipsy', 'title'=>$view_report_title));
	echo CHtml::link($new_offer_btn, Yii::app()->createUrl('/offer/form', array('sid'=>$r['id'])), array('class'=>'btn btn-mini', 'rel'=>'tipsy', 'title'=>$new_offer_title));
	echo CHtml::link($delete_btn, Yii::app()->createUrl('/offer/delete', array('id'=>$r['id'])), array('class'=>'btn btn-mini delete-btn', 'rel'=>'tipsy', 'title'=>$delete_title));
	echo '</div>';
}
?>
	</td>
</tr>
<?php
}
echo '</table>';

//pagination
echo '<div role="grid" class="dataTables_wrapper"><div class="row-fluid"><div class="span12"><div class="dataTables_paginate paging_bootstrap pagination">';
$this->widget('CLinkPager', array(
	'currentPage' => $pages->getCurrentPage(),
	'itemCount' => $pages->getItemCount(),
	'pageSize' => $pages->getPageSize(),
	'prevPageLabel' => '&lt;',
	'nextPageLabel' => '&gt;',
	'firstPageLabel' => '&lt;&lt;',
	'lastPageLabel' => '&gt;&gt;',
	'maxButtonCount' => 5,
	'header' => '',
	'selectedPageCssClass' => 'active',
	'htmlOptions' => array('class'=>'page-ul'),
));
echo '</div></div></div></div>';
?>
<script type="text/javascript">
$(function() {
	$('.hover-action').mouseenter(function(){
		//$(this).find('.hover-show').show();
	}).mouseleave(function(){
		//$(this).find('.hover-show').hide();
	});
	
	$('.page-ul a').click(function(){
		$.ajax({
			'beforeSend':beforeSend, 'success':success,
			'type':'POST', 'url':$(this).attr('href'), 'data':$('#search_form').serialize()
		});
		return false;
	});
	
	$('.popup-btn').facebox({
		loadingImage: '<?php echo Yii::app()->baseUrl ?>/img/ajax-loaders/ajax-loader-4.gif',
		closeImage: '<?php echo Yii::app()->baseUrl ?>/img/btn-close.png',
		overlayClose:true, width:360
	});
	
	$('.delete-btn').click(function() {
		if (confirm('<?php echo BUtils::T('delete_btn_confirm'); ?>')) {
			var _this = $(this);
			if (_this.attr('disabled')) {
				return false;
			}
			jQuery.ajax({
				'type':'GET',
				'url':_this.attr('href'),
				'beforeSend':function() {
					_this.attr('disabled','disabled').addClass('button-disabled');
				},
				'success':function(html) {
					_this.removeAttr('disabled').removeClass('button-disabled');
					_this.parent().parent().parent().fadeOut();
				}
			});
		}
		return false;
	});
	$('.publish-btn').click(function() {
		if (confirm('<?php echo BUtils::T('publish_btn_confirm'); ?>')) {
			var _this = $(this);
			if (_this.attr('disabled')) {
				return false;
			}
			jQuery.ajax({
				'type':'GET',
				'url':_this.attr('href'),
				'beforeSend':function() {
					_this.attr('disabled','disabled').addClass('button-disabled');
				},
				'success':function(html) {
					_this.removeAttr('disabled').removeClass('button-disabled');
					_this.parent().children('.unpub').hide();
				}
			});
		}
		return false;
	});
	$('.fb-like-callback').click(function(){
		fbUI.share($(this).data('url'));
	});
	$('[rel=tipsy]').tooltip({placement:'top'});
});
</script>